package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrgroupWithattrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.experimental.PackagePrivate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelation;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<AttrGroupEntity>();
        if(StringUtils.isNotBlank(key)){
            queryWrapper.and((obj)->{
                obj.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }else{
            queryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    //获取分类下所有分组&关联属性
    @Override
    public List<AttrgroupWithattrVo> getAttrgroupWithattrByCategoryId(Long catelogId) {
        //查找分类的全部属性分组
        List<AttrGroupEntity> attrGroupEntitys = attrGroupService.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId,catelogId));

        //查询属性分组内的全部属性
        List<AttrgroupWithattrVo> attrgroupWithattrVoList = attrGroupEntitys.stream().map(item -> {
            AttrgroupWithattrVo attrgroupWithattrVo = new AttrgroupWithattrVo();

            BeanUtils.copyProperties(item,attrgroupWithattrVo);
            List<AttrEntity> relationAttrs = attrAttrgroupRelation.getRelationAttr(item.getAttrGroupId());
            attrgroupWithattrVo.setAttrs(relationAttrs);
            if(CollectionUtils.isEmpty(relationAttrs)){
               return null;
            }
            return attrgroupWithattrVo;
        }).collect(Collectors.toList());

        return attrgroupWithattrVoList;
    }


}